package org.example.RabbitListener.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//这个配置需要开启重试机制才会开启
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener.simple.retry",name="enabled",havingValue = "true")
public class ErrorConfig
{
	@Bean
	public DirectExchange errorExchange()
	{
		return new DirectExchange("error.direct");
	}
	
	@Bean
	public Queue errorQueue()
	{
		return new Queue("error.queue");
	}
	
	@Bean
	public Binding errorBinding(DirectExchange errorExchange,Queue errorQueue)
	{
		//队列绑定交换机
		return BindingBuilder.bind(errorQueue).to(errorExchange).with("error");
	}
	
	@Bean
	public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate)
	{
		//配置发送错误后将消息转移
		return new RepublishMessageRecoverer(rabbitTemplate,"error.direct","error");
	}
}